// pages/component/devices/device.js
const app = getApp();

const api = require('../../../apis/request.js');
Component({
  pageLifetimes: {
    show: function () {
      // 页面被展示
      this.refresh()
      // console.log('123')
    },
    hide: function () {
      // 页面被隐藏
      clearInterval(this.data.inter_id)
    },
    resize: function (size) {
      // 页面尺寸变化
    }
  },
  /**
   * 组件的属性列表
   */
  properties: {
    gSerial: {
      type: String,
      value: ""
    },
    deviceRegion: {
      type: String,
      value: ""
    },
    regionName: {
      type: String,
      value: ""
    },


  },
  attached() {
    // console.log(132)
    var this_ = this
    api.listRegionByGserial(this.data.gSerial, function(data) {
      let {
        data: res
      } = data
      // console.log(this_.data.Serial_obj)
      // console.log(this_.data)
      var obj = {
        gSerial: this_.data.gSerial,
        deviceRegion: this_.data.deviceRegion,
        regionName: this_.data.regionName
      }
      this_.setData({
        position: res.datas,
        Serial_obj: obj
      })
      // console.log(this_.data.Serial_obj, 222222222222222233333333)

    })
  },
  /**
   * 组件的初始数据
   */
  data: {
    position: [1, 2, 3],
    Serial_obj: {},
    deviceList: [],
    // 定时器id
    inter_id:''
  },

  /**
   * 组件的方法列表
   */
  // show() {
   
  // },
  methods: {
    // 定时刷新数据

    refresh() {
      this.data.inter_id = setInterval(() => {
        this.device_hq()

        wx.setStorage({
          key: "inter_id",
          data: this.data.inter_id
        })

      }, 5000)
    },
    // 选择框传值
    select_gserial(a) {
      // console.log(a.detail)
      // this.triggerEvent("devices_gserial", a.detail);
      // wx.setStorage({
      //   key: "gserial",
      //   data: a.detail
      // })
      this.data.Serial_obj = a.detail
      console.log(this.data.Serial_obj)
      this.device_hq()
      setTimeout(() => {
        for (var i = 0; i < this.data.deviceList.length; i++) {
          this.data.deviceList[i].se_lock = true
          this.selectComponent("#list").device_icon(this.data.deviceList[i]);
          // console.log(this.selectComponent("#list"),1111)

        }

      }, 1000)
    },

    // 获取设备方法
    device_hq() {
      var _this = this;
      // console.log(_this.data.gSerial + "\n" + _this.data.deviceRegion + "\n" + _this.data.regionName);
      api.listDeviceByRegion(_this.data.Serial_obj.gSerial, _this.data.Serial_obj.deviceRegion, function(res) {
        var data = res.data;
          
        if (data.status != 0) {

          return;
        }
        // console.log(data,'获取第一次设备定时状况')
        var currentControlType;
        if (data.datas[0].controlType == '03') {
          currentControlType = '01';
        } else {
          currentControlType = data.datas[0].controlType;
        }
        console.log("初始化控制方式 = " + currentControlType + "--" + data.datas[0].deviceRegion);
        // _this.setData({ deviceList: data.datas, groupType: data.datas[0].groupType, currentControlType: currentControlType });
        _this.data.groupType = data.datas[0].groupType;

        data.datas.forEach(item => {
          item.iconPath = _this.iconPath_method(item).iconPath
          item.iconPath_gif = _this.iconPath_method(item).iconPath_gif
          item.iconPath_x = _this.iconPath_method(item).iconPath_x
          item.e_id = escape(JSON.stringify(item))
          // console.log(item.iconPath)
        })

        _this.setData({
          deviceList: [],
          deviceList: data.datas,
          currentControlType: currentControlType
        });
        for (var i = 0; i < _this.data.deviceList.length; i++) {
          // this.data.deviceList[i].se_lock = true
          _this.selectComponent("#list").device_icon(_this.data.deviceList[i]);

        }
        // console.log(_this.data.deviceList)
        
      });
    },

    switchCtrlType: function(event) {
      console.log('switchCtrlType', JSON.stringify(event));
      var _this = this;
      if (_this.data.currentControlType == '00') {
        _this.setData({
          currentControlType: _this.data.currentControlType
        });
        wx.showModal({
          showCancel: false,
          title: '操作无效:现在不能进行远程操作',
          confirmText: '好的'
        });
        return;
      }
      if (event.detail.value == '00') {
        _this.setData({
          currentControlType: _this.data.currentControlType
        });
        wx.showModal({
          showCancel: false,
          title: '操作无效:只能从现场切换本地控制',
          confirmText: '好的'
        });

        return;
      }

      api.switchCtrlTypeByGroup(_this.data.Serial_obj.gSerial, _this.data.Serial_obj.deviceRegion, _this.data.groupType, event.detail.value, function(res) {
        var data = res.data;
        if (data.status != 0) {
          _this.setData({
            currentControlType: _this.data.currentControlType
          });
          wx.showToast({
            title: '设备不在线'
          });
          return;
        }
        var modalContent;
        switch (event.detail.value) {
          case '00':
            //本地
            modalContent = '本地'
            break;
          case '01':
            //手动
            modalContent = '手动'
            break;
          case '02':
            //逻辑
            modalContent = '逻辑'
            break;
        }

        wx.showModal({
          content: '切换' + modalContent + '控制方式成功',
          showCancel: false,
          confirmText: '好的'
        });
        console.log("event.detail.value = " + event.detail.value);
        _this.setData({
          currentControlType: event.detail.value
        });
        console.log("重新渲染currentControlType = " + _this.data.currentControlType);
      });

    },
    iconPath_method(device) {
      // console.log(device,132132132112)
      var _this = this
      var iconPath = "../../../img/device/d_fan.png";
      var iconPath_x = "../../../img/device/d_fan.png";
      var iconPath_gif = '../../../img/device/tupian/d_fan.gif'
      // console.log(device.deviceType)
      switch (device.deviceType) {
        case '00':
          iconPath = "../../../img/device/d_fan.png";
          iconPath_x = "../../../img/device/d_fan.png";
          iconPath_gif = '../../../img/device/tupian/d_fan.gif'
          break;
        case '01':

          iconPath = "../../../img/device/d_cycle_fan.png";
          iconPath_x = "../../../img/device/d_cycle_fan.png";
          iconPath_gif = '../../../img/device/tupian/d_cycle_fan.gif'

          break;
        case '02':
          iconPath = "../../../img/device/d_sky_window.png";
          iconPath_x = "../../../img/device/d_sky_window.png";
          iconPath_gif = '../../../img/device/tupian/d_sky_window.gif'

          break;
        case '03':
          iconPath = "../../../img/device/d_side_window.png";
          iconPath_x = "../../../img/device/d_side_window.png";
          iconPath_gif = '../../../img/device/tupian/d_side_window.gif'

          break;
        case '04':
          iconPath = "../../../img/device/d_out_shadow.png";
          iconPath_x = "../../../img/device/d_out_shadow.png";
          iconPath_gif = '../../../img/device/tupian/d_out_shadow.gif'

          break;
        case '05':
          iconPath = "../../../img/device/d_in_shadow.png";
          iconPath_x = "../../../img/device/d_in_shadow.png";
          iconPath_gif = '../../../img/device/tupian/d_in_shadow.gif'

          break;
        case '06':
          iconPath = "../../../img/device/d_cool_pad.png";
          iconPath_x = "../../../img/device/d_cool_pad.png";
          iconPath_gif = '../../../img/device/tupian/d_cool_pad.gif'

          break;
        case '07':
          iconPath = "../../../img/device/d_water_pump.png";
          iconPath_x = "../../../img/device/d_water_pump.png";
          iconPath_gif = '../../../img/device/tupian/d_water_pump.gif'

          
          break;
        case '08':
          iconPath = "../../../img/device/d_sol_value.png";
          iconPath_x = "../../../img/device/d_sol_value.png";
          iconPath_gif = '../../../img/device/tupian/d_sol_value.gif'

          
          break;
        case '09':
          iconPath = "../../../img/device/d_solenoid_value.png";
          iconPath_x = "../../../img/device/d_solenoid_value.png";
          iconPath_gif = '../../../img/device/tupian/d_solenoid_value.gif'

          
          break;
        case '10':
          iconPath = "../../../img/device/d_light.png";
          iconPath_x = "../../../img/device/d_light.png";
          iconPath_gif = '../../../img/device/tupian/d_light.gif'

          
          break;
        case '11':
          iconPath = "../../../img/device/d_heat.png";
          iconPath_x = "../../../img/device/d_heat.png";
          iconPath_gif = '../../../img/device/tupian/d_heat.gif'

          
          break;
        case '12':
          iconPath = "../../../img/device/d_gas.png";
          iconPath_x = "../../../img/device/d_gas.png";
          iconPath_gif = '../../../img/device/tupian/d_gas.gif'

          
          break;
        case '13':
          iconPath = "../../../img/device/d_roll_film.png";
          iconPath_x = "../../../img/device/d_roll_film.png";
          iconPath_gif = '../../../img/device/tupian/d_roll_film.gif'

          
          break;
        case '14':
          iconPath = "../../../img/device/d_roll.png";
          iconPath_x = "../../../img/device/d_roll.png";
          iconPath_gif = '../../../img/device/tupian/d_roll.gif'

          
          break;
      }
      // _this.setData({
      //   iconPath: iconPath
      // });
      return { iconPath: iconPath, iconPath_gif: iconPath_gif, iconPath_x: iconPath_x}
    },

  },
  lifetimes: {

    ready: function() {

      
      setTimeout(() => {
          this.device_hq()
        clearInterval(this.data.inter_id)

        this.refresh()

      // setTimeout(() => {

      //   for (var i = 0; i < this.data.deviceList.length; i++) {
      //     // this.data.deviceList[i].se_lock = true
      //     this.selectComponent("#list").device_icon(this.data.deviceList[i]);

      //     }
        },1000)


      // }, 1000)
    }
  }

})